package com.regan.erp.model;


import cn.hutool.core.text.StrFormatter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.regan.erp.util.Constants;

/**
 * 返回结果信息
 *
 * @author zhangby
 * @date 2017/11/30 下午7:09
 */
public class ResultPoJo {
    private String code;
    private String msg;
    private Object result;

    public ResultPoJo() {
        this.code = Constants.NORMAL;
    }

    //链式构造
    public static ResultPoJo create() {
        ResultPoJo poJo = new ResultPoJo();
        poJo.code = Constants.NORMAL;
        poJo.msg = Constants.MSG_000;
        return poJo;
    }

    //链式构造，增加错误信息
    public static ResultPoJo create(String num) {
        ResultPoJo poJo = new ResultPoJo();
        poJo.code = Constants.error_code(num);
        poJo.msg = Constants.error_msg(num);
        return poJo;
    }

    //是否成功
    public boolean success(){
    	return this.code.equals(Constants.NORMAL);
    }


    //链式构造，增加错误信息
    public static ResultPoJo create(String num,Object... msg) {
        ResultPoJo poJo = new ResultPoJo();
        poJo.code = Constants.error_code(num);
        poJo.msg = StrFormatter.format(Constants.error_msg(num), msg);
        return poJo;
    }

    public ResultPoJo code(String code) {
        this.code = code;
        return this;
    }
    public ResultPoJo msg(String msg) {
        this.msg = msg;
        return this;
    }

    public ResultPoJo result(Object result) {
        this.result = result;
        return this;
    }

    /**
     * 数据转换，消除fastjson驼峰
     * @return
     */
    public String toFastJson() {
        SerializeConfig config = new SerializeConfig();
        config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
        return JSON.toJSONString(this, config, null, "yyyy-MM-dd HH:mm:ss", 1);
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Object getResult() {
        return result;
    }

}
